import Vue from 'vue'
import VueRouter from 'vue-router'
Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/layout'
  },
  {
    path: '/login',
    name: 'login',
    component: () => import('@/views/Login/Login.vue')
  },
  {
    path: '/layout',
    name: 'layout',
    component: () => import('@/views/Layout/Layout.vue'),
    redirect: '/layout/home',
    children: [
      { path: 'home', name: 'home', component: () => import('@/views/Home/Home') },
      // 懒加载组件 不是es6语法是webpack提供的
      { path: 'addArticle', name: 'addArticle', component: () => import('@/views/Article/ArticleAdd') },
      { path: 'articleList', name: 'articleList', component: () => import('@/views/Article/ArticleList') },
      { path: 'image', name: 'image', component: () => import('@/views/Images/Images') },
      { path: 'comment', name: 'comment', component: () => import('@/views/Comment/Comment') },
      { path: 'fans', name: 'fans', component: () => import('@/views/Fans/Fans') },
      { path: 'settings', name: 'settings', component: () => import('@/views/Settings/Settings') }

    ]
  },
  { path: '*', name: 'page404', component: () => import('@/views/404/404') }
]

const router = new VueRouter({
  routes
})

// 导航守卫 判断有无权限，再决定跳转
router.beforeEach((to, from, next) => {
  if (to.path !== '/login' && !sessionStorage.getItem('token')) {
    next('/login')
  } else {
    next()
  }
})

export default router
